{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# -*- coding: utf-8 -*-\n",
    "\n",
    "# 独立使用django model（一）\n",
    "# Django Shell\n",
    "# 项目根目录中打开Django Shell : python manage.py shell\n",
    "# import django\n",
    "# django.setup()  # 装载Django\n",
    "# 调用model\n",
    "# django 快速入门 数据操作 https://blog.csdn.net/u011054333/article/details/78767724\n",
    "\n",
    "# 独立使用django model（二）\n",
    "import sys\n",
    "import os\n",
    "import json\n",
    "import requests\n",
    "from django.core.files import File\n",
    "from django.core.files.base import ContentFile\n",
    "\n",
    "\n",
    "# 建立外部脚本链接django项目\n",
    "# 添加环境变量\n",
    "'''\n",
    "print(os.path.abspath('__file__'))\n",
    "print(os.path.dirname(os.path.abspath('__file__')))\n",
    "print(os.path.dirname(os.path.dirname(os.path.abspath('__file__'))))\n",
    "/Users/zhaojinhui/Desktop/webapp/backend/rmis/jupyter_notebook/__file__\n",
    "/Users/zhaojinhui/Desktop/webapp/backend/rmis/jupyter_notebook\n",
    "/Users/zhaojinhui/Desktop/webapp/backend/rmis\n",
    "'''\n",
    "project = os.path.dirname(os.getcwd())  # get current work directory\n",
    "sys.path.append(project)\n",
    "sys.path.append(os.path.join(project,'rmis'))\n",
    "os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings')\n",
    "# sys.path.append已设置临时环境变量，可以找到rmis.settings，脚本是外部脚本，只是放在了项目当中而已\n",
    "# 相关数据库配置等在settings.py文件中\n",
    "\n",
    "# 导入并装载django\n",
    "import django\n",
    "django.setup()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "type(datetime.now()) <class 'datetime.datetime'> 2019-04-08 08:31:37.783960\n",
      "type(time.time()) <class 'float'> 1554683497.784211\n",
      "type(some_good.created) <class 'datetime.datetime'> 2019-03-16 10:21:37.461916\n",
      "type(some_good.created.timestamp()) <class 'float'> 1552702897.461916\n",
      "2019-04-07 09:09:59.016837\n",
      "2019-04-07\n"
     ]
    }
   ],
   "source": [
    "# python 时间模块time，datetime\n",
    "# https://www.cnblogs.com/wj-1314/p/8533137.html\n",
    "\n",
    "'''\n",
    "一、在Python中，通常有这几种方式来表示时间：\n",
    "\n",
    "1.时间戳\n",
    "2.格式化的时间字符串\n",
    "3.元组（struct_time）共九个元素（由于Python的time模块实现主要调用C库，所以各个平台可能有所不同）。\n",
    "'''\n",
    "\n",
    "from good.models import Good\n",
    "from datetime import datetime, date\n",
    "import time\n",
    "\n",
    "print('type(datetime.now())', type(datetime.now()),datetime.now())\n",
    "print('type(time.time())', type(time.time()),time.time())\n",
    "\n",
    "some_good = Good.objects.get(id=86)\n",
    "print('type(some_good.created)', type(some_good.created), some_good.created)\n",
    "print('type(some_good.created.timestamp())',type(some_good.created.timestamp()), some_good.created.timestamp())  # 精确到微秒，区别于保留六位小数\n",
    "\n",
    "print(datetime.fromtimestamp(1554599399.0168371))\n",
    "print(date.fromtimestamp(1554599399.0168371))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
